package com.bbgwap.controller;


import com.alibaba.fastjson.JSON;
import com.bbgkh.model.BaseInfo;
import com.bbgkh.model.PO.CustomerPO;
import com.bbgkh.service.IUserService;
import com.bbgkh.utils.BBLogger;
import com.bbgkh.utils.MD5Utils;
import com.bbgkh.utils.StringUtils;
import com.bbgkh.utils.UserUtils;
import com.bbgwap.model.WeChatInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.bbgwap.base.APPCONST.*;


/**
 * Created by lixiang01 on 1/16/2017.
 */
@RestController
@CrossOrigin
public class HomeController extends BaseController{

    @Autowired
    private IUserService userService;


    @PostMapping(value = {"user/login"})
    public String userLogin(HttpServletRequest request,
                            HttpServletResponse response,
                            @RequestBody CustomerPO customerPO){

        logger.info("验证登录名，用户输入的用户名为："+customerPO.getName()+",密码为："+customerPO.getPassword());
        BaseInfo baseInfo = null;
        List<CustomerPO> list=  userService.validateUser(customerPO.getName(),customerPO.getPassword());
        if(list.size()>0){
            baseInfo = new BaseInfo("0","登录成功");
            String uid = StringUtils.getUID(customerPO.getName());
            Cookie cookie = new Cookie("uid",uid);
            cookie.setPath("/");
            cookie.setMaxAge(7200);
            response.addCookie(cookie);
            request.getSession(true).setAttribute("customer",customerPO);
        }else {
            baseInfo = new BaseInfo("100","登录失败");
        }

        return JSON.toJSONString(baseInfo);

    }

    @GetMapping(value = {"user/setOpenId"})
    public String setOpenId(@RequestParam(required = false, defaultValue = "0") String backType ,
                            HttpServletRequest request,
                            HttpServletResponse response){

        BaseInfo baseInfo =null;

        if(backType.equals("1")){

            WeChatInfo weChatInfo = (WeChatInfo) request.getSession().getAttribute(WECHAT_INFO);
            CustomerPO customer = new CustomerPO();
            customer.setName(weChatInfo.getOpenid());
            customer.setPassword(weChatInfo.getOpenid());
            userService.insert(customer);
            baseInfo = new BaseInfo("0","成功注册店员");

        }else{
            String backUrl  = WEBROOT+"/user/setOpenId?backType=1";
            request.getSession().setAttribute(BACK_URL,backUrl);
            try {
                response.sendRedirect(WECHAT_OPENID);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return JSON.toJSONString(baseInfo);
    }



}
